const express = require("express");
const router = express.Router();
const expressJoi = require("@escook/express-joi")
const filesHandler = require('../router_handler/files')
const iconv = require('iconv-lite');

// 导入解析 formdata 格式表单数据的包
const multer = require('multer')
var storage = multer.diskStorage({
    //设置上传后文件路径，uploads文件夹会自动创建。
    destination: function (req, file, cb) {
        cb(null, path.join(__dirname, '../public/files'))
    },
    /**
     * 给上传文件重命名，获取添加后缀名
     * 参数名 + 文件名 + 时间戳
     * */
    filename: function (req, file, cb) {
        var fileFormat = (file.originalname).split(".");

        //  返回文件名
        cb(null,  iconv.decode(fileFormat[0] + '_' + Date.now() + "." + fileFormat[fileFormat.length - 1],'utf-8'));
    }
});
// 导入处理路径的核心模块
const path = require('path')
// 创建 multer 的实例对象，通过 dest 属性指定文件的存放路径
// const upload = multer({ dest: path.join(__dirname, '../public/uploads') })
const upload = multer({
    storage: storage
});

router.post('/uploadFiles', upload.single('files'), filesHandler.uploadFiles)
router.post('/getcontractList', filesHandler.getcontractList)

router.post('/deleteContract', filesHandler.deleteContract)
router.post('/downloadFiles', filesHandler.downloadFiles)
router.post('/updateDownloadNumber', filesHandler.updateDownloadNumber)
module.exports = router